#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : lei.wang
import uuid as uuid_pkg
import pytz
from datetime import datetime
from typing import Optional
from sqlalchemy import String, DateTime, Integer
from sqlalchemy.orm import Mapped, mapped_column
from app.core.db.database import Base
from app.core.config import config


time_zone = pytz.timezone(config.get("timezone", default='Asia/Shanghai'))


class Users(Base):
    """ 用户信息 """
    __tablename__ = "users"

    id: Mapped[int] = mapped_column("id", autoincrement=True, nullable=False, unique=True, primary_key=True, init=False, comment='主键id')
    uuid: Mapped[uuid_pkg.UUID] = mapped_column(default_factory=uuid_pkg.uuid4, nullable=True, index=True, comment='业务uuid')
    payload_id: Mapped[str] = mapped_column(String(255), nullable=True, default='', comment='用户标识')
    birthday: Mapped[str] = mapped_column(String(255), nullable=True, default='',  comment='出生日期')
    gender: Mapped[str] = mapped_column(String(255), nullable=True, default='', comment='性别')
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default_factory=lambda: datetime.now(time_zone), comment='创建时间')
    updated_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), default=None, comment='更新时间')
    is_deleted: Mapped[bool] = mapped_column(default=False, index=True, comment='是否删除')

    username: Mapped[str] = mapped_column(String(255), unique=True, nullable=True, default=None, comment='用户昵称')
    password: Mapped[str] = mapped_column(String(255), nullable=True, default='', comment='密码')


class UserScore(Base):
    """ 用户积分信息 """
    __tablename__ = "user_score"

    id: Mapped[int] = mapped_column("id", autoincrement=True, nullable=False, unique=True, primary_key=True, init=False, comment='主键id')
    payload_id: Mapped[str] = mapped_column(String(255), nullable=True, default='', comment='用户标识')
    total_score: Mapped[int] = mapped_column(Integer, nullable=False, default=0, comment='总积分')
    used_score: Mapped[int] = mapped_column(Integer, nullable=False, default=0, comment='已使用积分')
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default_factory=lambda: datetime.now(time_zone), comment='创建时间')
    updated_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), default=None, comment='更新时间')
    is_deleted: Mapped[bool] = mapped_column(default=False, index=True, comment='是否删除')
